
<!DOCTYPE html>
<html>
<head>
<!-- This is separate page for registration/ adding users to DB. Probably can be implemented as div.
 -->	
 <title>Register Yourself</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<!-- <link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
	integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
	crossorigin="anonymous"> -->
	<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
	<style type="text/css">
		body { font: 14px sans-serif; }

		.wrapper {
			width:350px;
			padding: 20px;
			}
	</style>
</head>
<body>
	<?php
	require 'header.php';
// create_db  should be somewere on landing page and  executed only once
	require_once 'create_db.php'; 
	require_once 'connect.php';
	

	$password_err = $confirm_password_err = $login_err = "";
    
    if($_SERVER["REQUEST_METHOD"] == "POST"){

    	//validate password
		if(empty(trim($_POST["password"]))){
			$password_err = "Please type a password";
		}
		elseif(strlen(trim($_POST["password"])) < 4){
			$password_err = "Password length should be minimum 4";
		}
		else{
			$password = trim($_POST["password"]);
		}

		//validate confirm password
		if(empty(trim($_POST["confirm_password"]))){
			$confirm_password_err = "Please retype the password";
		}
		else
		{
			$confirm_password = trim($_POST["confirm_password"]);
			if($password != $confirm_password){
			    $confirm_password_err = "Password Does not match!";
			}
		}

		//Setting login and type variables
		// Default user type is "customer". "admin" should be redirected

		$type = "customer";

		$login_try = trim($_POST["login"]);
        $sql = "select * from users where login ='$login_try'";
		$result = $conn->query($sql);

		if(empty(trim($_POST["login"]))){
			$login_err = "Please type your login";
		}
		elseif($result->num_rows > 0){	
                $login_err = 'Login already exists! Try different one.';
        }
        else{
        	$login = $login_try;
        }

        //Checking the errors before inserting data into database
		if(empty($login_err) && empty($password_err) && empty($confirm_password_err)){

			//prepare the insert statement
			$sql = "INSERT INTO users (pass, login, type) VALUES (?,?,?)";
			
			//create the prepared statment
			$stmt = $conn->prepare($sql);

			//Bind variables to the prepared statement
			$stmt->bind_param("sss", $param_pass, $login, $type);

			//set parameter values	
			$param_pass = password_hash($password, PASSWORD_DEFAULT);
			//this takes last created id from $sql
			$user_id = $conn->insert_id;


			//execute the prepared statement		
			if($stmt->execute()){
				
				session_start();
				$_SESSION['login'] = $login;
				$_SESSION['user_id'] = $user_id;

				header("location: welcome.php");
			}
			else
			{
				echo "We are facing some problem! Try again please...";
			}

			$stmt->close();			
			}

			$conn->close();		
		}     
?>

<!-- Registration Form -->
<div id="register-wrapper">
	<div id="register-form" class="wrapper">
		<h2 class="text-white">Sign Up</h2>
		<p class="text-white">Please fill this form to create an Account. </p>

		<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
			<div class="form-group <?php echo (!empty($login_err))?'has error':''?>">
				<label class="text-white">Login</label>
				<input type="text" name="login"  class="form-control <?php echo $name; ?>">
				<span class="help-block"><?php echo $login_err; ?></span>
			</div>
			<div class="form-group <?php echo (!empty($password_err))?'has error':''?>">
				<label class="text-white">Password</label>
				<input type="password" name="password" class="form-control <?php echo $password; ?>">
				<span class="help-block"><?php echo $password_err; ?></span>
			</div>
			<div class="form-group <?php echo (!empty($confirm_password_err))?'has error':''?>">
				<label class="text-white">Confirm Password</label>
				<input type="password" name="confirm_password"  class="form-control <?php echo $confirm_password; ?>">
				<span class="help-block"><?php echo $confirm_password_err; ?></span>
			</div>
			<div style="text-align:center;" class="form-group">
				<input class="btn-max btn-size" type="submit" class="btn btn-primary" value="Submit">
				<input class="btn-max btn-size" type="reset" class="btn btn-default" value="Reset">
			</div>
			<p class="text-white">Already have an account? <a class="text-white" href="login.php">Login here</a></p>

		</form>

	</div>
</div>

</body>
</html>